<template>
  <div class="mod--retailProd">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
      @selection-change="handleSelectionChange"
    >
      <template slot="menuLeft">
        <el-button
          icon="el-icon-plus"
          type="primary"
          size="small"
          @click="addOrUpdateHandle()"
          v-if="isAuth('shop:retailProd:save')"
          >{{ $t("product.addNew") }}</el-button
        >
        <el-button
          icon="el-icon-upload2"
          type="primary"
          size="small"
          v-if="isAuth('shop:retailProd:import')"
          @click="inportRetailProd()"
          >{{ $t("retailProd.import") }}</el-button
        >
        <el-button
          icon="el-icon-download"
          type="primary"
          size="small"
          @click="exportRetailProd()"
          v-if="isAuth('shop:retailProd:export')"
        >{{ $t("formData.export") }}</el-button
        >
        <el-button
          type="danger"
          @click="deleteHandle()"
          size="small"
          v-if="isAuth('shop:retailProd:delete')"
          :disabled="dataListSelections.length <= 0"
          >{{ $t("sys.batchDelete") }}</el-button
        >
      </template>
      <!-- <template slot-scope="scope" slot="pic"></template> -->
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="primary"
          v-if="isAuth('shop:retailProd:update')"
          @click="addOrUpdateHandle(scope.row.retailProdId)"
          >{{ $t("temp.modify") }}</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          v-if="isAuth('shop:retailProd:delete')"
          @click="deleteHandle(scope.row.retailProdId)"
          >{{ $t("text.delBtn") }}</el-button
        >
      </template>
    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>
    <retail-prod-upload
      v-if="uploadVisible"
      ref="retailProdUpload"
      @refreshUpload="refreshUpload"
    ></retail-prod-upload>
  </div>
</template>

<script>
import { tableOption } from '@/crud/prod/retailProd'
import AddOrUpdate from './retailProd-add-or-update'
import RetailProdUpload from './retail-prod-upload'

export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {}, // 搜索
      dataListSelections: [],
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false,
      uploadVisible: false
    }
  },
  components: {
    AddOrUpdate,
    RetailProdUpload
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/shop/retailProd/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.retailProdId
      })
      this.$confirm(`${this.$i18n.t('sys.makeSure')}[${id ? this.$i18n.t('text.delBtn') : this.$i18n.t('sys.batchDelete')}]${this.$i18n.t('text.menu')}?`, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/shop/retailProd'),
          method: 'delete',
          // data: this.$http.adornData({})
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page.currentPage = 1
      this.getDataList(this.page)
    },
    refreshUpload () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 多选变化
    handleSelectionChange (val) {
      if (val.length > 1) {
        this.dataListSelections = val
      } else {
        this.dataListSelections = []
      }
    },
    /**
     * 导入单品
     */
    inportRetailProd () {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.retailProdUpload.init()
      })
    },
    /**
     * 导出单品
     */
    exportRetailProd () {
      this.$http({
        url: this.$http.adornUrl('/shop/retailProd/exportRetailProd'),
        method: 'get',
        params: this.$http.adornParams(this.searchForm),
        responseType: 'blob' // 解决文件下载乱码问题
      }).then(({ data }) => {
        var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
        const fileName = '单品信息表.xls'
        const elink = document.createElement('a')
        if ('download' in elink) { // 非IE下载
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>
<style lang="scss">
.mod--retailProd {
  /deep/ .el-table__fixed-right {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  /deep/ .el-table__fixed{
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
}
</style>
